<template>
  <el-card class="filter-card" shadow="never">
    <el-form :model="filterParams" inline>
      <el-form-item label="交易状态">
        <el-select v-model="filterParams.transactionStatus" clearable placeholder="全部状态" class="form-item-width">
          <el-option label="已匹配" value="matched" />
          <el-option label="未匹配" value="unmatched" />
          <el-option label="待确认" value="pending" />
        </el-select>
      </el-form-item>
      
      <el-form-item label="付款方">
        <el-input
          v-model="filterParams.payerName"
          placeholder="输入付款方"
          clearable
          style="width: 150px"
        />
      </el-form-item>
      
      <el-form-item label="交易日期">
        <el-date-picker
          v-model="filterParams.transactionDateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      
      <el-form-item label="交易金额">
        <el-input-number
          v-model="filterParams.minAmount"
          :min="0"
          placeholder="最小金额"
          style="width: 120px"
        />
        <span class="separator">-</span>
        <el-input-number
          v-model="filterParams.maxAmount"
          :min="0"
          placeholder="最大金额"
          style="width: 120px"
        />
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" :icon="Search" @click="handleSearch">
          查询
        </el-button>
        <el-button :icon="Refresh" @click="handleReset">
          重置
        </el-button>
        <el-button 
          link
          :icon="SetUp" 
          @click="showAdvancedFilter = !showAdvancedFilter"
        >
          高级筛选
        </el-button>
      </el-form-item>
    </el-form>

    <!-- 高级筛选 -->
    <el-collapse-transition>
      <div v-show="showAdvancedFilter" class="advanced-filter">
        <el-divider content-position="left">高级筛选</el-divider>
        <el-form :model="filterParams" inline>
          <el-form-item label="银行名称">
            <el-input
              v-model="filterParams.bankName"
              placeholder="输入银行名称"
              clearable
              style="width: 150px"
            />
          </el-form-item>
          
          <el-form-item label="交易类型">
            <el-select v-model="filterParams.transactionType" clearable class="form-item-width">
              <el-option label="收入" value="income" />
              <el-option label="支出" value="expense" />
              <el-option label="转账" value="transfer" />
            </el-select>
          </el-form-item>
          
          <el-form-item label="导入批次">
            <el-input
              v-model="filterParams.importBatch"
              placeholder="输入导入批次号"
              clearable
              style="width: 180px"
            />
          </el-form-item>
          
          <el-form-item label="包含备注">
            <el-switch v-model="filterParams.hasRemark" />
          </el-form-item>
          
          <el-form-item label="重复流水">
            <el-switch v-model="filterParams.showDuplicates" />
          </el-form-item>
        </el-form>
      </div>
    </el-collapse-transition>
  </el-card>
</template>

<script setup lang="ts">
import { ref, reactive, defineProps, defineEmits, watch } from 'vue'
import { Search, Refresh, SetUp } from '@element-plus/icons-vue'

// 定义组件属性
interface FilterParams {
  transactionStatus: string
  payerName: string
  transactionDateRange: string[]
  minAmount: number | undefined
  maxAmount: number | undefined
  bankName: string
  transactionType: string
  importBatch: string
  hasRemark: boolean
  showDuplicates: boolean
}

const props = defineProps<{
  modelValue: FilterParams
}>()

const emit = defineEmits<{
  (e: 'update:modelValue', value: FilterParams): void
  (e: 'search'): void
  (e: 'reset'): void
}>()

// 创建本地副本以避免直接修改 props
const filterParams = reactive({ ...props.modelValue })

// 监听 props 变化并更新本地副本
watch(() => props.modelValue, (newVal) => {
  Object.assign(filterParams, newVal)
}, { deep: true })

// 监听本地副本变化并触发更新事件
watch(filterParams, (newVal) => {
  emit('update:modelValue', { ...newVal })
}, { deep: true })

const showAdvancedFilter = ref(false)

const handleSearch = () => {
  emit('search')
}

const handleReset = () => {
  // 重置本地副本
  Object.assign(filterParams, {
    transactionStatus: '',
    payerName: '',
    transactionDateRange: [],
    minAmount: undefined,
    maxAmount: undefined,
    bankName: '',
    transactionType: '',
    importBatch: '',
    hasRemark: false,
    showDuplicates: false
  })
  emit('reset')
}
</script>

<style scoped>
.filter-card {
  margin-bottom: 20px;
}

.advanced-filter {
  margin-top: 20px;
}

.separator {
  margin: 0 8px;
  color: #909399;
}
</style>